<?php

/**
 * @filename TableView.class.php 
 * @encoding UTF-8 
 * @author nemo.xiaolan <a href="mailto:335454250@qq.com">335454250@qq.com</a>
 * @link <a href="http://www.sep-v.com">http://www.sep-v.com</a>
 * @license http://www.sep-v.com/code-license
 * @datetime 2013-12-4  11:47:02
 * @Description
 * 
 */
class TableView {
    
    protected $hasTable = false;
    
    protected $tableStruct;
    
    public function __construct($model, $action="", $groupName = "") {
        $groupName = $groupName ? $groupName : GROUP_NAME;
        if($action) {
            $file = sprintf("%s/Conf/TableView/%s/%s%s.php", ENTRY_PATH,$groupName, $model, $action);
        } else {
            $file = sprintf("%s/Conf/TableView/%s/%s.php", ENTRY_PATH,$groupName, $model);
        }
        
        if(file_exists_case($file)) {
            $this->tableStruct = require($file);
            $this->hasTable = true;
        }
    }
    
    public function makeHorizontalTable($data, $pages) {
        if(!$this->hasTable) {
            return false;
        }
        
        $tableTemplate = '<table %s>%s%s%s</table>';
        $theadTemplate = '<thead><tr>%s</tr></thead>';
        $tbodyTemplate = '<tbody>%s</tbody>';
        $tfootTemplate = '<tfoot>%s</tfoot>';
        $fieldsNum = count($this->tableStruct["fields"]);
        
        foreach($this->tableStruct["fields"] as $field) {
            list($field, $decorate)=explode("|", $field);
            if($decorate) {
                $decorate($field);
            }
            /**
             * sortable:
             * request: sort(asc 1, desc 2), order(byField)
             * **/
            if($this->tableStruct["sortable"]) {
                $sortField = $this->tableStruct["sortAlias"][$field] ? $this->tableStruct["sortAlias"][$field] : $field;
                if($_REQUEST["order"] == $sortField) {
                    $sort = $_REQUEST["sort"] ? 0 : 1;
                    $sortClass = $sort ? "icon-arrow-up" : "icon-arrow-down";
                    $icon = sprintf(' <i class="icon %s"></i> ', $sortClass);
                } else {
                    $sort = 0;
                    $icon = "";
                }
                
                $thead[] = sprintf('<th><a href="javascript:sortBy(\'%s\', %d)">%s</a>%s</th>', $sortField, $sort, L($field), $icon);
            } else {
                $thead[] = sprintf('<th>%s</th>', L($field));
            }
        }
        if($this->tableStruct["actions"]) {
            $thead[] = sprintf('<th>%s</th>', L("operation"));
        }
        
        foreach($data as $k=>$v) {
            
            $tmp = '<tr>%s</tr>';
            $tds = array();
            if($this->tableStruct["checkbox"]) {
                $fieldsNum++;
            }
            foreach($this->tableStruct["fields"] as $field) {
                list($fi, $decorate)=explode("|", $field);
                if($decorate) {
                    $v[$fi] = $decorate($v[$fi]);
                }
                $tds[] = sprintf('<td>%s</td>', $v[$field]);
            }
            $itemActions = array();
            foreach($this->tableStruct["actions"] as $label => $a) {
                list($act, $class, $condition) = explode("|", $a);
                if($condition) {
                    list($cf, $va) = explode("=", $condition);
                    if($v[$cf] != $va) {
                        continue;
                    }
                }
                if($class) {
                    $icon = sprintf('<i class="icon icon-%s"></i> ', $class);
                } else {
                    $icon = L($label);
                }
                
                if(substr($act, 0, 11) == "javascript:") {
                    $itemActions[] = sprintf(sprintf('<a href="#nogo" onclick="%s" title="%s"> %s </a> ', $act,  L($label), $icon), $v["id"]);
                } else {
                    $itemActions[] = sprintf('<a href="%s?id=%d" title="%s"> %s </a> ', U($act), $v["id"], L($label), $icon);
                }
            }
            $tds[] = sprintf('<td>%s</td>', implode(" ", $itemActions));
            $tbody[] = sprintf($tmp, implode('', $tds));
        }
        
        /**
         * 使用默认table样式
         */
        if(false !== $this->tableStruct["useDefaultTheme"]) {
            $this->tableStruct["tableAttr"]["class"] .= "table table-bordered table-striped with-check";
        }
        
        foreach($this->tableStruct["tableAttr"] as $k=>$v) {
            $tableAttr[] = sprintf('%s="%s"', $k, $v);
        }
        
        
        if($this->tableStruct["actions"]) {
            $fieldsNum++;
        }
        
        if($this->tableStruct["pages"]) {
            $pages = sprintf('<div class="pagination alternate"><ul>%s</ul></div>', $pages);
            $tfoot[] = sprintf($tfootTemplate, sprintf('<tr><td colspan="%s">%s</td></tr>', 
                    $fieldsNum, $pages)
            );
        }
        
        $html = sprintf($tableTemplate,
                implode(" ", $tableAttr),
                sprintf($theadTemplate, implode("", $thead)),
                sprintf($tbodyTemplate, implode("", $tbody)),
                sprintf($tfootTemplate, implode("", $tfoot))
            );
//        var_dump($html);exit;?
        return $html;
    }
    
}

?>
